package moodish.client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;

/**
 * This class create a JDialog which allows the user to choose the ImageIcon.
 * that represents his/her Mood.
 * 
 * @author es1-2013-group2
 *
 */
public class MoodChooser extends JDialog{

	private User user;
	private UserPanel panel;

	
	/**
	 * The constructor receives the User and a UserPanel.
	 * Creates a grid with different ImageIcons.
	 * It sets an action listener to define the user Mood when the correspondent button is pressed.
	 * 
	 * @param user User to set the Mood.
	 * @param panel UserPanel.
	 */
	public MoodChooser(final User user, final UserPanel panel) {
		
		this.user = user;
		this.panel = panel;
		
		this.setLayout(new GridLayout(Mood.values().length/Mood.values().length/2, Mood.values().length/2));	
		for (final Mood m : Mood.values()) {
			JButton button = new JButton(m.getImageIcon());
			this.add(button);
			button.setToolTipText(m.name());
			button.setBackground(Color.white);
			button.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					user.setMood(m);
					dispose();
					panel.updateUserState();
				}
			});
		}
	
		this.setTitle("Choose your mood");
		this.setLayout(new GridLayout(Mood.values().length/Mood.values().length/2, Mood.values().length/2));
		this.getContentPane().setPreferredSize(new Dimension(Mood.Happy.getImageIcon().getIconWidth()*
		Mood.values().length/2 + getWidth()-getContentPane().getWidth(), Mood.Happy.getImageIcon().getIconHeight()* 2 + getHeight()-getContentPane().getHeight()));
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
				
	}
	
}
